<?php
class page{
    private $count_page = 0;
    private $page = 1;
    private $page_num = 20;
    public function __construct($count,$page,$page_num = 0){
        //每页的记录条数
        if(!empty($page_num) && is_numeric($page_num)){
            $this->page_num = $page_num;
        }
        //总页数
        $this->count_page = ceil($count/$this->page_num);
        //当前页码
        if($page < 1){
            $page = 1;
        }
        if($page >= $this->count_page){
            $page = $this->count_page;
        }
        $this->page = $page;
    }
    public function getPage(){
        $page = $this->page;
        $first_page = 1;
        if($page <= 1){
            $prev_page = 1;
        }else{
            $prev_page = $page-1;
        }
        if($page >= $this->count_page){
            $next_page = $this->count_page;
        }else{
            $next_page = $page+1;
        }
        $last_page = $this->count_page;
        if($page <= 1){
            $str = "<div class='pageBox'><a href='javascript:;'>首页</a> |
            <a href='javascript:;'>上一页</a> |
            <a href='?page={$next_page}'>下一页</a> |
            <a href='?page={$last_page}'>尾页</a></div>";
        }else if($page >= $this->count_page){
            $str = "<div class='pageBox'><a href='?page={$first_page}'>首页</a> | 
                <a href='?page={$prev_page}'>上一页</a> | 
                <a href='javascript:;'>下一页</a> | 
                <a href='javascript:;'>尾页</a></div>";
        }else{
            $str = "<div class='pageBox'><a href='?page={$first_page}'>首页</a> |
            <a href='?page={$prev_page}'>上一页</a> |
            <a href='?page={$next_page}'>下一页</a> |
            <a href='?page={$last_page}'>尾页</a></div>";
        }
        return $str;
    }
    public function getPageBlock($num = 5){
        $show_num = $num * 2 + 1;
        $count_page = $this->count_page;
        $page = $this->page;
        if($count_page <= $show_num){
            $start = 1;
            $end = $count_page;
        }else{
            if($page - $num<=0){
                $start = 1;
                $end = $show_num;
            }elseif($page + $num >= $count_page){
                $start = $count_page - $show_num +1;
                $end = $count_page;
            }else{
                $start = $page - $num;
                $end = $page + $num;
            }
        }
        $get_str = $this->getGETValue();
        $url = $this->getUri();
        if($page == 1){
            $href_first='javascript:void()';
        }else {
            $href_first="{$url}-page-1.html";
        }
        if($page == $count_page){
            $href_last='javascript:void()';
        }else {
            $href_last="{$url}-page-{$count_page}.html";
        }
        $pageStr = '<div class="pageBox"><ul class="pagination"><li><a href="'.$href_first.'">首页</a></li>';
        for($i=$start;$i<=$end;$i++){
            if($page == $i){
                $pageStr .= "<li class='active'><a href='javascript:;'>{$i}</a></li>";
            }else{
                $pageStr .= "<li><a href='{$url}-page-{$i}.html'>{$i}</a></li>";
            }
        }
        $pageStr .= "<li><a href='{$href_last}'>尾页</a></li></div>";
        return $pageStr;
    }
    private function getGETValue(){
        $arr = $_GET;
        $str = '';
        foreach ($arr as $key=>$value){
            if($key != 'page'){
                $str .= $key.'='.$value.'&';
            }
        }
        return $str;
    }
    private function getUri(){
        $uri = $_SERVER['REQUEST_URI'];
        $str = str_replace('.html', '', $uri);
        $str1 = preg_replace('/-page-\d*/', '', $str);
        return $str1;
    }
}